import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class QueryForm {
    
    
    public static double stopGPSlatitude;
    public static double stopGPSlongitude;
    private static int transportType;
    private static int transportRoute;
    private static int routeDirection;
    private static int stopID;
    private static int weekDays;
    private static String weekDaysChoice = " ";
    
    public static List<Coordinate> getRouteStopList(int transportType, int transportRoute, int transportDirection, boolean weekDay) {
        
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document xmlDB = null;
        try {
            builder = builderFactory.newDocumentBuilder();
        }catch(ParserConfigurationException e) {
            e.printStackTrace();
        }
        
        try {
            xmlDB = builder.parse(new FileInputStream("WIMI-DB-4.xml"));
        }catch(SAXException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
        
        XPath entryPath = XPathFactory.newInstance().newXPath();
        
        String choice = "";

        if(transportType == 0)
            choice = "tramvajs";
        else if(transportType == 1)
            choice = "autobuss";
        else if(transportType == 2)
            choice = "trolejbuss";
        
        String query = "/entries/" + choice + "[@id=\'"
                + transportRoute + "\']/virziens[@id=\'"
                + transportDirection + "\']/pietura[position() <= last()]";
        
        String queryValue = "";
        List<Coordinate> finalWDList = new ArrayList<Coordinate>();
        List<Coordinate> finalHDList = new ArrayList<Coordinate>();
        
        Element tempNode;
        
        try {
            // queryValue = entryPath.compile(queryWD).evaluate(xmlDB);
                    //+ entryPath.compile(queryHD).evaluate(xmlDB);
            
            NodeList nodeList = (NodeList) entryPath.compile(query).evaluate(xmlDB, XPathConstants.NODESET);
            for(int i = 0; i < nodeList.getLength(); i++) {
                
                String tempName = (nodeList.item(i).getFirstChild().getNodeValue());
                
                tempNode = (Element) nodeList.item(i);
                String tempID = (tempNode.getAttributes().getNamedItem("id").getNodeValue());
                
                String queryGPSlatitude = "/entries/" + choice + "[@id=\'"
                + transportRoute + "\']/virziens[@id=\'"
                + transportDirection + "\']/pietura[@id=\'" + tempID + "\']/garums";
                
                String queryGPSlongitude = "/entries/" + choice + "[@id=\'"
                + transportRoute + "\']/virziens[@id=\'"
                + transportDirection + "\']/pietura[@id=\'" + tempID + "\']/platums";
                
                NodeList latList = (NodeList) entryPath.compile(queryGPSlatitude).evaluate(xmlDB, XPathConstants.NODESET);
                NodeList lonList = (NodeList) entryPath.compile(queryGPSlongitude).evaluate(xmlDB, XPathConstants.NODESET);
                
                String tempLat = latList.item(0).getTextContent();
                double newGPSlat = Double.parseDouble(tempLat);
                
                String tompLat = lonList.item(0).getTextContent();
                double newGPSlon = Double.parseDouble(tompLat);
                
                String queryWDTimes = "/entries/" + choice + "[@id=\'"
                + transportRoute + "\']/virziens[@id=\'"
                + transportDirection + "\']/pietura[@id=\'" + tempID
                        + "\']/darbdienas/laiks[position() <= last()]";
                
                String queryHDTimes = "/entries/" + choice + "[@id=\'"
                + transportRoute + "\']/virziens[@id=\'"
                + transportDirection + "\']/pietura[@id=\'" + tempID
                        + "\']/brivdienas/laiks[position() <= last()]";
                
                NodeList queryWDList = (NodeList) entryPath.compile(queryWDTimes).evaluate(xmlDB, XPathConstants.NODESET);
                NodeList queryHDList = (NodeList) entryPath.compile(queryHDTimes).evaluate(xmlDB, XPathConstants.NODESET);
                
                List<String> wdTimeList= new ArrayList<String>();
                List<String> hdTimeList = new ArrayList<String>();
                for(int j=0;j<queryWDList.getLength();j++){
                    String s = queryWDList.item(j).getTextContent();
                    wdTimeList.add(s);
                }
                
                for(int e = 0;e < queryHDList.getLength(); e++){
                    String d = queryHDList.item(e).getTextContent();
                    hdTimeList.add(d);
                }
                
                Coordinate wdKaste = new Coordinate(newGPSlat, newGPSlon);
                wdKaste.setName(tempName);
                wdKaste.setTime(wdTimeList);
                finalWDList.add(wdKaste);
                
                Coordinate hdKaste = new Coordinate(newGPSlat, newGPSlon);
                hdKaste.setName(tempName);
                hdKaste.setTime(hdTimeList);
                finalHDList.add(hdKaste);
                
                // System.out.println(latList.item(i).getTextContent());
                
            }
                // GPSlist.add(nodeList.item(i).getFirstChild().getNodeValue());
            
            // queryValue = "Darbdienas: " + queryValue;
        }catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println(finalWDList.size());
        // return finalWDList;
        
        if(weekDay) return finalWDList;
        else return finalHDList;
        
    }
    
    
    
    public static void main(String[] args) {
        QueryForm standalone = new QueryForm();
        standalone.getRouteStopList(1, 1, 1, true);
    }
}